package com.gzhryc.shared_device.cdb.core.protocol.v1;

import java.util.regex.Matcher;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.shared_device.cdb.core.CDBConstants;
import com.gzhryc.shared_device.cdb.core.ICDBNetworkService;
import com.gzhryc.shared_device.cdb.core.models.CDBExecuteResult;
import com.gzhryc.shared_device.cdb.core.models.CDBLeaseCommand;
import com.gzhryc.shared_device.cdb.core.protocol.v1.msg.BWRequest;
import com.gzhryc.shared_device.cdb.core.protocol.v1.msg.CQRequest;
import com.gzhryc.shared_device.cdb.core.protocol.v1.msg.FARequest;
import com.gzhryc.shared_device.cdb.core.protocol.v1.msg.FBRequest;
import com.gzhryc.shared_device.iot.core.IOTClient;

public class V1_CDBNetworkService implements ICDBNetworkService {

    static Logger log = Logger.getLogger(V1_CDBNetworkService.class);

    private final IOTClient iotClient;

    public V1_CDBNetworkService(IOTClient iotClient){
        this.iotClient = iotClient;
    }

    @Override
    public CDBExecuteResult lease(CDBLeaseCommand cdbLeaseCommand) {
        CDBExecuteResult result = new CDBExecuteResult(CDBExecuteResult.FAIL);
        try {
            String cmd = null;
            String content = null;

            if(cdbLeaseCommand.getSlotNum() == null){
                //获取当前卡槽

            }

            if(cdbLeaseCommand.getSlotNum() != null){
                FBRequest fbRequest = new FBRequest();
                fbRequest.setTradeNo(cdbLeaseCommand.getTradeNo());
                fbRequest.setSlotNum(cdbLeaseCommand.getSlotNum());
                cmd = fbRequest.getCmd();
                content = fbRequest.getMessage();
                if(StringTools.isNotBlank(cdbLeaseCommand.getMessageId())) {
                    fbRequest.setMessageId(cdbLeaseCommand.getMessageId());
                }else{
                    cdbLeaseCommand.setMessageId(fbRequest.getMessageId());
                }
            }else {
                BWRequest bwRequest = new BWRequest();
                bwRequest.setTradeNo(cdbLeaseCommand.getTradeNo());
                cmd = bwRequest.getCmd();
                content = bwRequest.getMessage();
                if(StringTools.isNotBlank(cdbLeaseCommand.getMessageId())) {
                    bwRequest.setMessageId(cdbLeaseCommand.getMessageId());
                }else{
                    cdbLeaseCommand.setMessageId(bwRequest.getMessageId());
                }
            }

            V1_CDB_IOTMessage message = new V1_CDB_IOTMessage(content);
            message.setMessageId(cdbLeaseCommand.getMessageId());
            message.setDeviceSn(cdbLeaseCommand.getDeviceSn());
            message.setCmd(cmd);
            message.setTopic("/" + cdbLeaseCommand.getDeviceSn() + "/user/get");

            if (iotClient.send(message)) {
                result.setCode(CDBExecuteResult.SUCCESS);
                result.setMessageId(message.getMessageId());
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDBExecuteResult forceEjectDevice(String deviceSn, Integer slotNum,String messageId) {
        CDBExecuteResult result = new CDBExecuteResult(CDBExecuteResult.FAIL);
        try {
            FARequest faRequest = new FARequest();
            faRequest.setSlotNum(slotNum);
            if(StringTools.isNotBlank(messageId)){
                faRequest.setMessageId(messageId);
            }

            V1_CDB_IOTMessage message = new V1_CDB_IOTMessage(faRequest.getMessage());
            message.setMessageId(faRequest.getMessageId());
            message.setDeviceSn(deviceSn);
            message.setCmd(faRequest.getCmd());
            message.setTopic("/" + deviceSn + "/user/get");

            if (iotClient.send(message)) {
                result.setCode(CDBExecuteResult.SUCCESS);
                result.setMessageId(message.getMessageId());
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDBExecuteResult forceEjectDevice(String deviceSn,String messageId) {
        CDBExecuteResult result = new CDBExecuteResult(CDBExecuteResult.FAIL);
        try {
            FARequest faRequest = new FARequest();
            faRequest.setSlotNum(0);
            if(StringTools.isNotBlank(messageId)){
                faRequest.setMessageId(messageId);
            }

            V1_CDB_IOTMessage message = new V1_CDB_IOTMessage(faRequest.getMessage());
            message.setMessageId(faRequest.getMessageId());
            message.setDeviceSn(deviceSn);
            message.setCmd(faRequest.getCmd());
            message.setTopic("/" + deviceSn + "/user/get");

            if (iotClient.send(message)) {
                result.setCode(CDBExecuteResult.SUCCESS);
                result.setMessageId(message.getMessageId());
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDBExecuteResult searchDevice(String deviceSn) {
        CDBExecuteResult result = new CDBExecuteResult(CDBExecuteResult.FAIL);
        try {
            CQRequest cqRequest = new CQRequest();
            cqRequest.setSlotNum(0);

            V1_CDB_IOTMessage message = new V1_CDB_IOTMessage(cqRequest.getMessage());
            message.setMessageId(cqRequest.getMessageId());
            message.setDeviceSn(deviceSn);
            message.setCmd(cqRequest.getCmd());
            message.setTopic("/" + deviceSn + "/user/get");

            if (iotClient.send(message)) {
                result.setCode(CDBExecuteResult.SUCCESS);
                result.setMessageId(message.getMessageId());
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setMsg(e.getMessage());
        }
        return result;
    }

    @Override
    public CDBExecuteResult sendCmd(String deviceSn, String content) {
        CDBExecuteResult result = new CDBExecuteResult(CDBExecuteResult.FAIL);
        try {
            String cmd = null;
            String msgId = null;
            Matcher mat = CDBConstants.CmdPattern.matcher(content);
            if (mat.find()) {
                cmd = mat.group(1);
                //消息ID
                String[] data = mat.group(2).split(",");
                msgId = data[0];
            }

            V1_CDB_IOTMessage message = new V1_CDB_IOTMessage(content);
            message.setMessageId(msgId);
            message.setDeviceSn(deviceSn);
            message.setCmd(cmd);
            message.setTopic("/" + deviceSn + "/user/get");

            if (iotClient.send(message)) {
                result.setCode(CDBExecuteResult.SUCCESS);
                result.setMessageId(message.getMessageId());
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            result.setMsg(e.getMessage());
        }
        return result;
    }
}
